<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="120px"
    >
    <el-form-item label="名单类型" prop="listType">
        <el-select
          v-model="queryParams.listType"
          style="width: 215px"
          placeholder="请选择名单类型"
          filterable
          clearable
          size="small"
        >
          <el-option
            v-for="dict in listTypeList"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="关联id" prop="relId">
        <el-input
          v-model="queryParams.relId"
          @keyup.enter.native="handleQuery"
          clearable
          style="width: 215px"
          placeholder="请输入关联id"
        />
      </el-form-item>
      <el-form-item label="关联编码" prop="relCode">
        <el-input
          style="width: 215px"
          v-model="queryParams.relCode"
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入关联编码"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          style="width: 215px"
          placeholder="请选择状态"
          filterable
          clearable
          size="small"
        >
          <el-option
            v-for="dict in statusList"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          v-hasPermi="['bac:sysListConfig:add']"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @handleQuery="handleQuery"
        @resetQuery="resetQuery"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="sysListConfigList"
      v-adaptive="{ bottomOffset: 50 }"
      height="100px"
    >
      <el-table-column
        label="名单类型"
        min-width="120"
        align="center"
        prop="listType"
      >
        <template slot-scope="scope">
          <span>{{ statue(scope.row.listType, listTypeList) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="关联id"
        min-width="120"
        align="center"
        prop="relId"
      >
      </el-table-column>
      <el-table-column
        label="关联编码"
        min-width="120"
        align="center"
        prop="relCode"
      >
      </el-table-column>
      <el-table-column
        label="备注"
        min-width="120"
        align="center"
        prop="remark"
        show-overflow-tooltip
      />
      <el-table-column
        label="创建人"
        min-width="120"
        align="center"
        prop="createBy"
        show-overflow-tooltip
      />
      <el-table-column
        label="创建时间"
        min-width="140"
        align="center"
        prop="createTime"
        show-overflow-tooltip
      />
      <el-table-column label="更新人" align="center" prop="updateBy"> </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime"> </el-table-column>
      <el-table-column
        label="状态"
        min-width="80"
        align="center"
        prop="status"
        fixed="right"
      >
        <!-- <template slot-scope="scope">
          <span>{{ statue(scope.row.status, statusList) }}</span>
        </template> -->
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="10"
            :inactive-value="20"
            @change="condition(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="100"
        align="center"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            style="margin-right: 10px"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleDeletel(scope.row)"
            style="margin-right: 10px"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加名单配置对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="550px"
      append-to-body
      @close="closeDialog"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="18">
            <el-form-item
              label="名单类型"
              prop="listType"
              :rules="{
                required: true,
                message: '请选择名单类型',
                trigger: 'change',
              }"
            >
              <el-select
                v-model="form.listType"
                style="width: 250px"
                placeholder="请选择名单类型"
                filterable
                clearable
                size="small"
              >
                <el-option
                  v-for="dict in listTypeList"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row>
          <el-col :span="16">
            <el-form-item
              label="状态"
              prop="status"
              :rules="{
                required: true,
                message: '请选择状态',
                trigger: 'change',
              }"
            >
              <el-select
                v-model="form.status"
                style="width: 250px"
                placeholder="请选择状态"
                filterable
                clearable
                size="small"
              >
                <el-option
                  v-for="dict in statusList"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row> -->
        <el-row>
          <el-col :span="16">
            <el-form-item
              label="关联id"
              prop="relId"
              :rules="{
                required: form.relCode ? false : true,
                message: '请输关联id',
                trigger: 'blur',
              }"
            >
              <el-input
                style="width: 250px"
                v-model="form.relId"
                clearable
                placeholder="请输入关联id"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="16">
            <el-form-item
              label="关联编码"
              prop="relCode"
              :rules="{
                required: form.relId ? false : true,
                message: '请输关联编码',
                trigger: 'blur',
              }"
            >
              <el-input
                style="width: 250px"
                v-model="form.relCode"
                clearable
                placeholder="请输入关联编码"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="16">
            <el-form-item label="备注" prop="remark">
              <el-input
                type="textarea"
                style="width: 250px"
                v-model="form.remark"
                placeholder="请输入备注"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div v-if="activate"></div>
  </div>
</template>

<script>
import {
  sysListConfigList,
  sysListConfigAdd,
  sysListConfigUpdate,
  updateListConfigStatus,
  sysListConfigDelete,
} from "@/api/common/listConfiguration";
export default {
  name: "Label",
  components: {},
  data() {
    return {
      open: false,
      title: "",
      activate: true,
      // 遮罩层
      loading: false,
      form: {},
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 表格数据
      sysListConfigList: [],
      //菜单名称
      listTypeList: [],
      //状态
      statusList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
    };
  },
  created() {
    // 类型
    this.getDicts("sys_list_config_type").then((res) => {
      this.listTypeList = res.data;
    });
    // 状态
    this.getDicts("sys_list_config_status").then((res) => {
      this.statusList = res.data;
    });
    this.getList();
  },
  //触发回流
  activated() {
    this.activate = false;
    setTimeout(() => {
      this.activate = true;
    });
    this.getList();
  },
  methods: {
    /** 查询商品信息列表 */
    getList() {
      this.loading = true;
      sysListConfigList(this.queryParams).then((response) => {
        this.sysListConfigList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      };
      this.handleQuery();
    },
    submitForm() {
      console.log(this.form);
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            sysListConfigUpdate(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            sysListConfigAdd({ ...this.form, status: 10 }).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    //弹框关闭事件
    closeDialog() {
      //清空表单校验
      this.$refs.form.resetFields();
      this.form = {};
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.title = "名单配置新增";
      this.open = true;
    },
    //状态
    condition(v) {
      updateListConfigStatus({ id: v.id, status: v.status })
        .then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
          });
          this.getList();
        })
        .catch((err) => {
          this.getList();
        });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      row.listType = row.listType.toString()
      this.form = { ...row };
      this.title = "名单配置修改";
      this.open = true;
    },
    handleDeletel(row) {
      console.log(row);
      this.$confirm(
        "是否确认删除名单配置名称为 “" + this.statue(row.listType, this.listTypeList) + "” 的数据项？",
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        sysListConfigDelete(row.id).then((response) => {
          this.getList();
          this.msgSuccess("删除成功");
        });
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.treeselect-main {
  width: 193px;
}
</style>
